import ruleTypes from './type';

export default {
  // required
  required(message) {
    return {
      required: true,
      message
    };
  },

  // len
  len({ min, max, len }, message) {
    return {
      required: true,
      min,
      max,
      len,
      message
    };
  },

  // mobile
  mobile(message) {
    return {
      validator(rule, value, callback) {
        if (!ruleTypes.australia_mobile.reg.test(value)) {
          callback(message);
          return message;
        }

        callback();
      }
    };
  },

  // email
  email(message) {
    return {
      required: true,
      type: 'email',
      message
    };
  },

  // password
  passWord(message = 'The password must be at least 6 characters and at most 32 characters, and consist of an upper case letter, lower case letter and a number.') {
    const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,32}$/;
    return {
      validator(rule, value, callback) {
        if (!reg.test(value)) {
          callback(message);
          return message;
        }

        callback();
      }
    };
  },

  // letter
  letter(message) {
    const reg = /^[A-Za-z]+$/;
    return {
      validator(rule, value, callback) {
        if (!reg.test(value)) {
          callback(message);
          return message;
        }

        callback();
      }
    };
  },

  // driver
  driver(message) {
    return {
      validator(rule, value, callback) {
        if (!ruleTypes.driver.reg.test(value)) {
          callback(message);
          return message;
        }

        callback();
      }
    };
  }
};
